package tcp;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;

public class TesteServerSocket extends Thread {

    private ServerSocket sock;

    public TesteServerSocket() {
        try {
            sock = new ServerSocket(4413);
            System.out.println("Servidor rodando!");
        } catch (IOException e) {
            System.out.println("Erro: N�o pode criar o Socket");
        }
    }

    public void run() {
        Socket client = null;

        while (true) {
            if (sock == null) {
                return;
            }
            try {
                client = sock.accept();
                BufferedOutputStream bos = new BufferedOutputStream(client.getOutputStream());
                PrintWriter os = new PrintWriter(bos, false);

                Date now = new Date();
                os.println(now);
                os.flush();

                os.close();
                client.close();

                Thread.sleep(1000);
            } catch (Exception e) {
                System.out.println("Erro: N�o pode se conectar ao cliente!");
            }
        }
    }

    public static void main(String[] args) {
        TesteServerSocket server = new TesteServerSocket();
        server.start();
    }
}
